package org.martyn.danbodroid.database;

import org.martyn.danbodroid.logger.Logger;
import org.martyn.danbodroid.preferences.Preferences;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper
{

	public DatabaseHelper(Context context)
	{
		super(context, Preferences.DATABASE_NAME, null,
				Preferences.DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase database)
	{
		database.execSQL("CREATE TABLE " + ServersTable.TABLE_NAME + " ("
				+ ServersTable.COLUMN_NAME_ID + " INTEGER PRIMARY KEY,"
				+ ServersTable.COLUMN_NAME_NAME + " VARCHAR(255),"
				+ ServersTable.COLUMN_NAME_ADDRESS + " TEXT,"
				+ ServersTable.COLUMN_NAME_PARSETYPE + " VARCHAR(255));");
	}

	@Override
	public void onUpgrade(
			SQLiteDatabase database,
			int oldVersion,
			int newVersion)
	{
		// Logs that the database is being upgraded
		Logger.Verbose("", "Upgrading database from version " + oldVersion
				+ " to " + newVersion + ", which will destroy all old data");

		// Kills the table and existing data
		// database.execSQL("DROP TABLE IF EXISTS notes");

		// Recreates the database with a new version
		// onCreate(database);
	}

	public static void closeDatabase(SQLiteDatabase database)
	{
		if (database != null && database.isOpen())
		{
			database.close();
		}
	}
}
